﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System.Data.SqlClient;

namespace PoemsSystem
{
    public partial class MainForm : Form
    {
        bool CalDistance = false;
        bool ShowText = false;
        Coordinate coord1;
        Coordinate coord2;
        public static Coordinate location;
        public static bool IfDrawLocation = false;

        public void Combobox()
        {
            cbxAuthor.Items.Add("王维");
            cbxAuthor.Items.Add("李白");
        }

        public MainForm()
        {
            InitializeComponent();
            Combobox();
        }

        private void LoadMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            map.AddLayers();
        }

        private void ClearMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            map.ClearLayers();
        }

        private void SaveMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("确定退出程序？","退出程序",
                               MessageBoxButtons.OKCancel,
                               MessageBoxIcon.Question)
                               ==DialogResult.OK)
            {
                this.Close();
            }

        }

        private void toolStripSelect_Click(object sender, EventArgs e)
        {
            map.FunctionMode = FunctionMode.Select;
        }

        private void toolStripZoomToExtent_Click(object sender, EventArgs e)
        {
            map.ZoomToMaxExtent();
        }

        private void toolStripPan_Click(object sender, EventArgs e)
        {
            map.FunctionMode = FunctionMode.Pan;
        }

        private void toolStripZoomOut_Click(object sender, EventArgs e)
        {
            map.ZoomOut();
        }

        private void toolStripZoomIn_Click(object sender, EventArgs e)
        {
            map.ZoomIn();
        }

        private void toolStripInfo_Click(object sender, EventArgs e)
        {
            map.FunctionMode = FunctionMode.Info;
        }

        private void toolStripNone_Click(object sender, EventArgs e)
        {
            map.FunctionMode = FunctionMode.None;
        }

        private void toolStripLoadMap_Click(object sender, EventArgs e)
        {
            map.AddLayers();
        }

        private void toolStripClearMap_Click(object sender, EventArgs e)
        {
            map.ClearLayers();
        }

        private void toolStripSaveMap_Click(object sender, EventArgs e)
        {

        }

        private void map_MouseClick(object sender, MouseEventArgs e)
        {
            if (CalDistance == false)
            {
                coord1 = map.PixelToProj(e.Location);
                CalDistance = true;
            }
            else
            {
                coord2 = map.PixelToProj(e.Location);
                CalDistance = false;
            } 
        }
        private void Distance(Coordinate p1,Coordinate p2)
        {

        }

        private void InfoSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxAuthor.Text == "王维")
            {
                FilterForm selectForm = new FilterForm();
                selectForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("暂时无该诗人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void GraphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxAuthor.Text=="王维")
            {
                frmGraph graph = new frmGraph();
                graph.ShowDialog();
            }
            else
            {
                MessageBox.Show("暂时无该诗人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }    
        }

        private void legend_Click(object sender, EventArgs e)
        {

        }

        private void ShengpingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (map.Layers.Count==3)
            {
                return;
            }
            if (cbxAuthor.Text=="王维")
            {
                ShowText = true;
                map.AddLayer("疆域.shp");
                map.AddLayer("国家.shp");
                map.AddLayer("国界.shp");
                //map.AddLayer("Track_Line_output.shp");
                //map.AddLayer("Track_Point.shp");
                if (map.Layers.Count > 0)
                {
                    MapPolygonLayer stateLayer = default(MapPolygonLayer);
                    stateLayer = (MapPolygonLayer)map.Layers[0];
                    if (stateLayer == null)
                    {
                        MessageBox.Show("The layer is not a point layer.");
                    }
                    //else
                    //{
                    //    map.AddLabels(stateLayer, "[City]", new Font("黑体", (float)12.0), Color.Black);
                    //}
                }
                else
                {
                    MessageBox.Show("Please add a layer to the map.");
                }
                String connString = @"Data Source=MSI\SQLEXPRESS;
                                Initial Catalog=master;                            
                                Integrated Security=True";
                String commandTextQuery = "select year,anc_name,count(*) over() from dbo.guanzhi";

                using (SqlConnection connection = new SqlConnection(connString))
                {
                    connection.Open();
                    SqlCommand cmdQuery = new SqlCommand(commandTextQuery, connection);
                    SqlDataReader reader = cmdQuery.ExecuteReader();
                    string str = "";
                    string year0 = "";
                    string loc0 = "";
                    int i = 0;
                    int k = 0;
                    string str0 = "";

                    while (reader.Read())
                    {
                        string year = reader[0].ToString();
                        string loc = reader[1].ToString();
                        string num = reader[2].ToString();
                        k = int.Parse(num);
                        if (i == 0)
                        {
                            year0 = year;
                            loc0 = loc;
                        }

                        if (loc0 != loc)
                        {
                            str0 = year0 + "年-->" + year + "年\n" + loc0 + "\n";

                            str0 = str0 + year + "年\n" + loc0 + "-->" + loc + "\n";
                            year0 = year;
                            str = str + str0;
                        }
                        if (i == k - 1)
                        {
                            str0 = year0 + "年-->" + year + "年\n" + loc + "\n";
                            str = str + str0;
                        }
                        loc0 = loc;
                        i++;
                    }
                    rtbWangweiTrack.Text = str;
                    reader.Close();
                }
            }
            else
            {
                MessageBox.Show("暂时无该诗人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                map.ClearLayers();
                rtbIntro.Text = "";
                rtbWangweiTrack.Text = "";
            }
        }

        private void IntrotoolStrip_Click(object sender, EventArgs e)
        {
            
            if (cbxAuthor.Text=="王维")
            {
                String connString = @"Data Source=MSI\SQLEXPRESS;
                                Initial Catalog=C#;                            
                                Integrated Security=True";
                String commandTextQuery = "select intro from dbo.author";
                using (SqlConnection connection = new SqlConnection(connString))
                {
                    connection.Open();
                    SqlCommand cmdQuery = new SqlCommand(commandTextQuery, connection);
                    SqlDataReader reader = cmdQuery.ExecuteReader();
                    if (reader.Read())
                    {
                        string intro = reader[0].ToString();
                        rtbIntro.Text = intro;
                    }
                    reader.Close();
                }
            }
            else
            {
                MessageBox.Show("暂时无该诗人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                rtbIntro.Text = "";
                map.ClearLayers();
                rtbWangweiTrack.Text = "";
            }
        }

        private void InfoReactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InfoReact infoReact = new InfoReact();
            infoReact.ShowDialog();
        }

        private void map_Paint(object sender, PaintEventArgs e)
        {
            //画轨迹部分
            #region
            SqlDataReader reader = Database.ReadDataToDrawLine();
            List<DrawTracks.Track> tracks = new List<DrawTracks.Track>();
            DrawTracks.DrawData(reader, tracks);
            Graphics g = e.Graphics;
            Pen pen_Line = new Pen(Color.Red, 2);
            System.Drawing.Point p1 = new System.Drawing.Point();
            System.Drawing.Point p2 = new System.Drawing.Point();
            int i = 0;
            while (i < tracks.Count)
            {
                p1 = map.ProjToPixel(tracks[i].pstart);
                p2 = map.ProjToPixel(tracks[i].pend);
                g.DrawLine(pen_Line, p1, p2);
                i += 1;
            }
            #endregion
            if (ShowText)
            {
                //画点、画注释部分
                #region
                List<DrawTracks.Location> locations = new List<DrawTracks.Location>();
                Database.ReadDataToDrawText(locations);
                System.Drawing.Point point = new System.Drawing.Point();
                int j = 0;
                Font font = new Font("楷体", 12);
                SolidBrush brush_point = new SolidBrush(Color.Green);
                SolidBrush brush = new SolidBrush(Color.Blue);
                Size size = new Size(8, 8);
                Rectangle rectangle = new Rectangle();
                while (j < locations.Count)
                {
                    point = map.ProjToPixel(locations[j].location);
                    rectangle.Location = point;
                    rectangle.Size = size;
                    g.FillRectangle(brush_point, rectangle);
                    g.DrawString(locations[j].anc_name + '（' + locations[j].cur_name + '）',
                        font, brush, point.X - 45, point.Y - 13);
                    j += 1;
                }
                #endregion
            }
            if (IfDrawLocation)
            {
                //画选中的点的部分
                System.Drawing.Point point_select = new System.Drawing.Point();
                SolidBrush brush_location = new SolidBrush(Color.Orange);
                Size size_location = new Size(10, 10);
                Rectangle rectangle_location = new Rectangle();
                point_select = map.ProjToPixel(location);
                rectangle_location.X = point_select.X;
                rectangle_location.Y = point_select.Y;
                rectangle_location.Size = size_location;
                g.FillRectangle(brush_location, rectangle_location);
            }
        }
    }
}
